package com.zhixinhuixue.zsyte.xxx.app.api

import com.zhixinhuixue.zsyte.xxx.app.App
import com.zhixinhuixue.zsyte.xxx.data.response.TokenInfo
import com.zhixinhuixue.zsyte.xxx.data.response.UserInfo
import me.hgj.mvvmhelper.ext.logE
import me.hgj.mvvmhelper.util.SPUtil
import okhttp3.Interceptor
import okhttp3.Response
import okio.IOException

/**
 * 作者　: hegaojian
 * 时间　: 2021/6/9
 * 描述　:
 */
class HeadInterceptor : Interceptor {
    private var mTokenInfo: TokenInfo? = null
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val builder = chain.request().newBuilder()
        try {
            mTokenInfo = SPUtil.readObj(
                App.instance.applicationContext,
                SPUtil.FILE_NAME,
                SPUtil.USER_INFO
            ) as TokenInfo

            if (mTokenInfo!=null){
                builder.addHeader("Authorization", "${mTokenInfo!!.token_type} ${mTokenInfo!!.access_token}")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return chain.proceed(builder.build())
    }

}